/**
 *  Copyright 2002-2009 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package foo.bar.blog.core.domain;

import foo.bar.blog.radeox.Decoratable;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;
import java.util.UUID;
import java.util.LinkedHashSet;

/**
 * Represends a Blog's Entry domain object.
 *
 * @author tmjee
 * @version $Date: 2009-07-05 10:12:59 +0000 (Sun, 05 Jul 2009) $ $Id$
 */
@Entity
@EntityListeners({EntityAuditListener.class})
@Table(name="TBL_ENTRY")
@NamedQueries({
    @NamedQuery(name="entry_findEntryById",
        query="SELECT entry FROM Entry as entry WHERE entry.id=:id"),
    @NamedQuery(name="entry_findLatestPublishedEntries",
        query="SELECT entry FROM Entry as entry WHERE entry.state = :state ORDER BY entry.creationDate DESC"),
    @NamedQuery(name="entry_countEntryByNameAndBlog",
        query="SELECT count(entry) FROM Entry as entry WHERE entry.name = :name AND entry.blog = :blog")
})
public class Entry implements EntryInterface,CreationInfoPersistable, ModificationInfoPersistable, Identifiable, Decoratable {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "entryIdGenerator")
    @TableGenerator(name="entryIdGenerator", table = "TBL_ID_GENERATOR",
                    pkColumnName = "ID", valueColumnName = "ID_VALUE",
                    pkColumnValue = "TBL_ENTRY_CURRENT_ID", initialValue = 1,
                    allocationSize = 10)
    @Column(name="ID", nullable = false)
    private long id;


    @ManyToOne(targetEntity = Blog.class)
    @JoinColumn(table = "TBL_ENTRY", name = "BLOG_ID", referencedColumnName = "ID")
    private Blog blog;


    @OneToMany(targetEntity = Comment.class, cascade = {CascadeType.ALL}, mappedBy = "entry")
    @OrderBy("creationDate DESC")
    private Set<Comment> comments = new LinkedHashSet<Comment>();


    @Basic
    @Column(name="UID")
    private String uid = UUID.randomUUID().toString();


    @Basic
    @Column(name="NAME")
    private String name;

    @Basic
    @Column(name="DESCRIPTION")
    private String description;

    @Basic
    @Column(name="CONTENT")
    private String content;

    @Basic
    @Column(name="PUBLISH_DATE")
    private Date publishDate;

    @Basic
    @Column(name="STATE")
    @Enumerated(EnumType.STRING)
    private EntryState state;

    @Basic
    @Column(name="CREATION_DATE")
    private Date creationDate;

    @Basic
    @Column(name="MODIFICATION_DATE")
    private Date modificationDate;

    @ManyToOne(targetEntity = User.class)
    @JoinColumn(table = "TBL_ENTRY", name = "CREATOR_ID", referencedColumnName = "ID")
    private User creator;

    @ManyToOne(targetEntity = User.class)
    @JoinColumn(table = "TBL_ENTRY", name = "MODIFIER_ID", referencedColumnName = "ID")
    private User modifier;


    @ManyToOne(targetEntity = Tag.class)
    @JoinColumn(table="TBL_ENTRY", name="TAG_ID", referencedColumnName = "ID")
    private Tag tag;


    @Basic
    @Column(name="ALLOW_COMMENTS")
    private boolean allowComments = true;


    @Basic
    @Column(name="COMMENT_EXPIRY_DATE")
    private Date commentExpiryDate;

    @OneToMany(targetEntity = Resource.class, cascade = {CascadeType.ALL}, mappedBy = "entry")
    private Set<Resource> resources = new LinkedHashSet<Resource>();

    public void setResources(Set<Resource> resources) {
        this.resources = resources;
    }

    public Set<Resource> getResources() {
        return this.resources;
    }

    public Blog getBlog() {
        return blog;
    }

    public void setBlog(Blog blog) {
        this.blog = blog;
    }

    public void setTag(Tag tag) {
        this.tag = tag;
    }

    public Tag getTag() {
        return tag;
    }

    public Set<Comment> getComments() {
        return comments;
    }

    public void setComments(Set<Comment> comments) {
        this.comments = comments;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getPublishDate() {
        return publishDate;
    }

    public void setPublishDate(Date publishDate) {
        this.publishDate = publishDate;
    }

    public EntryState getState() {
        return state;
    }

    public void setState(EntryState state) {
        this.state = state;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreator(User creator) {
        this.creator = creator;
    }

    public User getCreator() {
        return creator;
    }

    public void setModificationDate(Date modificationDate) {
        this.modificationDate = modificationDate;
    }

    public Date getModificationDate() {
        return modificationDate;
    }

    public void setModifier(User modifier) {
        this.modifier = modifier;
    }

    public User getModifier() {
        return modifier;
    }

    public void setId(long id) {
        this.id = id;
    }

    public long getId() {
        return id;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }


    public boolean isAllowComments() {
        return allowComments;
    }

    public void setAllowComments(boolean allowComments) {
        this.allowComments = allowComments;
    }

    public Date getCommentExpiryDate() {
        return commentExpiryDate;
    }

    public void setCommentExpiryDate(Date commentExpiryDate) {
        this.commentExpiryDate = commentExpiryDate;
    }



    public boolean getCanComment() {
       return (allowComments || (commentExpiryDate != null && commentExpiryDate.after(new Date())));
    }



    public long getTotalComments() {
        if (comments != null)
            return comments.size();
        return 0;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Entry entry = (Entry) o;

        if (uid != null ? !uid.equals(entry.uid) : entry.uid != null) return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = (int) (id ^ (id >>> 32));
        result = 31 * result + (comments != null ? comments.hashCode() : 0);
        result = 31 * result + (uid != null ? uid.hashCode() : 0);
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (description != null ? description.hashCode() : 0);
        result = 31 * result + (content != null ? content.hashCode() : 0);
        result = 31 * result + (publishDate != null ? publishDate.hashCode() : 0);
        result = 31 * result + (state != null ? state.hashCode() : 0);
        result = 31 * result + (creationDate != null ? creationDate.hashCode() : 0);
        result = 31 * result + (modificationDate != null ? modificationDate.hashCode() : 0);
        result = 31 * result + (creator != null ? creator.hashCode() : 0);
        result = 31 * result + (modifier != null ? modifier.hashCode() : 0);
        result = 31 * result + (tag != null ? tag.hashCode() : 0);
        return result;
    }
}
